package org.ebookdroid.core;

import org.ebookdroid.R;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

public class PreloaderActivity extends Activity {
	private static final String packName = "org.ebookdroid";
	private static final long delay = 3000;

	private ImageView iv = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.preloader);

		PackageManager packMgr = getPackageManager();
		String versionName;
		try {
			PackageInfo packInfo = packMgr.getPackageInfo(packName, 0);
			versionName = packInfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			versionName = "1.0beta";
		}

		TextView tv = (TextView) findViewById(R.id.version);
		tv.setText("Version: " + versionName);

		new Handler().postDelayed(new Runnable() {
			public void run() {
				Intent intent = new Intent(PreloaderActivity.this,
						BrowserActivity.class);
				startActivity(intent);
				PreloaderActivity.this.finish();
			}
		}, delay);

		iv = (ImageView) findViewById(R.id.logoanim);
		iv.setBackgroundResource(R.anim.loading_pics);

	}

	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) 
			ad.start();
		else
			ad.stop();
	}
}